package com.phoenix.jsef.demo.service;

import com.phoenix.jsef.demo.dao.CVwOfficeDataAccess;
import com.phoenix.jsef.demo.pojo.CVwOfficeEntity;
import com.phoenix.jsef.demo.shared.IBizVwOffice;
import com.phoenix.jsef.framework.EntityCollection;
import com.phoenix.jsef.framework.PageInfo;
import com.phoenix.jsef.jdbcutils.CommandType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**   
 * @ClassName:  CBizVwOffice   
 * @Description:TODO(商业实体对象:CVwOfficeEntity对应提供的服务具体实现)   
 * @author: 李启联
 * @date:   2018-1-17 15:45:31  
 * @version V1.0     
 * @Copyright: Copyright (c) 2018
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
@Service
@CacheConfig(cacheNames="BizVwOffice_Cache")
public class CBizVwOffice implements IBizVwOffice {

	@Autowired
	private CVwOfficeDataAccess access;

	@Override
    @Cacheable()
	public EntityCollection getCollection(Map<String,Object> filter) throws Exception {
	    access.getAccessParameter().getFilter().cleanFilter();
		for(Map.Entry<String,Object> entry : filter.entrySet()){
			if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
				access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
		}
		return access.getCollection();
	}

	@Override
    @Cacheable()
	public List<CVwOfficeEntity> getList(Map<String,Object> filter) throws Exception {
		access.getAccessParameter().getFilter().cleanFilter();
		for(Map.Entry<String,Object> entry : filter.entrySet()){
			if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
				access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
		}
		return access.getList();
	}

    @Override
    @Cacheable()
    public List<CVwOfficeEntity> getListBySql(CommandType commandType, String commandText, Object... obj) throws Exception {
        return access.getListBySql(commandType,commandText,obj);
    }

    @Override
    @Cacheable(key="#root.targetClass+':'+#root.methodName+':'+#filter+':'+#pageIndex+':'+#pageSize")
    public synchronized PageInfo<CVwOfficeEntity> getPageInfo(Map<String, Object> filter, Map<String, String> sorter,
            Integer pageIndex, Integer pageSize) throws Exception {
        access.getAccessParameter().getFilter().cleanFilter();
        access.getAccessParameter().getSorter().clearSorter();

        if(null != filter) {
            for(Map.Entry<String,Object> entry : filter.entrySet()){
                if(entry.getValue() != null && entry.getValue().toString().trim().length() > 0)
                    access.getAccessParameter().getFilter().addEqual(entry.getKey(), entry.getValue());
            }
        }
        if(null != sorter) {
            for(Map.Entry<String,String> entry : sorter.entrySet()){
                if(entry.getValue() != null && entry.getValue().trim().length() > 0)
                    access.getAccessParameter().getSorter().add(entry.getKey(), entry.getValue());
                }
        }
        access.getAccessParameter().setPageSize(pageSize);
        access.getAccessParameter().setPageIndex(pageIndex);

        return access.getPageInfo();
    }

	@Override
    @Cacheable(key="#id")
	public CVwOfficeEntity getEntityById(String id) throws Exception {
		return access.getEntityById(id);
	}

	@Override
	public Map<String, Object> getMapById(String id) throws Exception {
		return access.getMapById(id);
	}

	@Override
	public boolean insert(CVwOfficeEntity entity) throws Exception {
		return access.insert(entity);
	}

	@Override
	public boolean update(CVwOfficeEntity entity) throws Exception {
		return access.update(entity);
	}

	@Override
	public boolean delete(CVwOfficeEntity entity) throws Exception {
		return access.delete(entity);
	}

	@Override
	public boolean deleteByIdForSingle(Object keyValue) throws Exception {
		return access.deleteByIdForSingle(keyValue);
	}

}